package net.itgoo.parkingpay.ui.orders;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import net.itgoo.parkingpay.R;
import net.itgoo.parkingpay.rest.model.ParkingOrderModel;
import net.itgoo.parkingpay.util.ParkingNumberUtils;

import java.util.List;

public class ParkingOrdersAdapter extends RecyclerView.Adapter<ParkingOrdersAdapter.RecyclerHolder> {
    private Context mContext;
    private List<ParkingOrderModel> mDataList;
    private OnItemClickListener mOnItemClickListener;

    public ParkingOrdersAdapter(Context context) {
        this.mContext = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setData(List<ParkingOrderModel> dataList) {
        if (null != dataList) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }

    @Override
    public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.parking_item_order, parent, false);
        return new RecyclerHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });

        ParkingOrderModel orderModel = mDataList.get(position);
        String[] dateTime = orderModel.orderTime.split(" ");

        holder.dateTextView.setText(dateTime[0]);
        holder.timeTextView.setText(dateTime[1]);
        holder.feeTextView.setText(ParkingNumberUtils.format(orderModel.amount, 2));
        holder.locationTextView.setText(orderModel.carPark);

        if (position % 2 == 0) {
            holder.rootView.setBackgroundColor(Color.WHITE);
        } else {
            holder.rootView.setBackgroundColor(Color.GRAY);
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class RecyclerHolder extends RecyclerView.ViewHolder {
        View rootView;
        TextView dateTextView;
        TextView timeTextView;
        TextView typeTextView;
        TextView feeTextView;
        TextView balTextView;
        TextView locationTextView;

        private RecyclerHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.parking_item_order_root);
            dateTextView = itemView.findViewById(R.id.parking_item_order_date_tv);
            timeTextView = itemView.findViewById(R.id.parking_item_order_time_tv);
            typeTextView = itemView.findViewById(R.id.parking_item_order_type_tv);
            feeTextView = itemView.findViewById(R.id.parking_item_order_fee_tv);
            balTextView = itemView.findViewById(R.id.parking_item_order_bal_tv);
            locationTextView = itemView.findViewById(R.id.parking_item_order_location_tv);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
}